package org.ssssssss.curd.strategy;

import org.apache.commons.lang3.StringUtils;
import org.ssssssss.curd.pojo.TableColumn;
import org.ssssssss.curd.pojo.TableMeta;
import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Mysql 获取表元信息策略
 */
public class MysqlGetTableMetaStrategy implements GetTableMetaStrategy{

    /**
     * 主键标记
     * @see TableColumn#getColumnKey()
     */
    private final static String PRIMARY_COLUMN_FLAG="PRI";
    /**
     * sql:列出表所有列元信息
     */
    private final static String LIST_TABLE_COLUMNS_SQL="select column_name,column_default,data_type,column_type," +
            " column_comment,column_key" +
            " from information_schema.COLUMNS" +
            " where table_name = ? and table_schema = database() " +
            " ORDER BY ordinal_position";
    /**
     * sql:列出当前数据库的所有表元信息
     */
    private final static String LIST_TABLE_SQL="SELECT table_schema,table_name,table_comment " +
            " FROM information_schema.TABLES " +
            " where table_schema = database()"+
            " ORDER BY create_time desc ";
    /**
     * magic-api 动态数据源对象 的数据源节点对象
     */
    private MagicDynamicDataSource.DataSourceNode dataSource;

    /**
     * 新建一个 {@link MysqlGetTableMetaStrategy} 对象
     * @param dataSource magic-api 动态数据源对象 的数据源节点对象
     */
    public MysqlGetTableMetaStrategy(MagicDynamicDataSource.DataSourceNode dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public List<TableColumn> listTableColumns(String tableName) {
        List<Map<String,Object>> tableColumnsMapList = dataSource.getJdbcTemplate().queryForList(LIST_TABLE_COLUMNS_SQL,tableName);
        return  tableColumnsMapList.stream().map(tableColumnsMap -> {
            TableColumn tableColumn = new TableColumn();
            tableColumn.setColumnName(String.valueOf(tableColumnsMap.get("column_name")));
            tableColumn.setColumnDefault(tableColumnsMap.get("column_default"));
            tableColumn.setDataType(String.valueOf(tableColumnsMap.get("data_type")));
            tableColumn.setColumnType(String.valueOf(tableColumnsMap.get("column_type")));
            tableColumn.setColumnComment(String.valueOf(tableColumnsMap.get("column_comment")));
            tableColumn.setColumnKey(String.valueOf(tableColumnsMap.get("column_key")));
            //如果 columnKey 是 PRI 就表示该列是主键
            tableColumn.setPrimary(StringUtils.equals(tableColumn.getColumnKey(),PRIMARY_COLUMN_FLAG));
            return tableColumn;
        }).collect(Collectors.toList());
    }

    @Override
    public List<TableMeta> listTables() {
        List<Map<String,Object>> tableMetaMapList = dataSource.getJdbcTemplate().queryForList(LIST_TABLE_SQL);
        return  tableMetaMapList.stream().map(tableColumnsMap -> {
            TableMeta tableColumn = new TableMeta();
            tableColumn.setTableSchema(String.valueOf(tableColumnsMap.get("table_schema")));
            tableColumn.setTableName(String.valueOf(tableColumnsMap.get("table_name")));
            tableColumn.setTableComment(String.valueOf(tableColumnsMap.get("table_comment")));
            return tableColumn;
        }).collect(Collectors.toList());
    }


}
